package com.anhuanjia.tools.eao.controller;

import java.util.Date;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoOperationLog;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;

@Alias(value = "operationLog")
public class OperationLogUI extends UIFacade {

    @Priv(loginType = LoginType.Member)
	public void loginOper() {
		Transaction transaction = new Transaction();
		EaoOperationLog o = new EaoOperationLog();
		o.setId(UUID.randomUUID().toString());
		o.setCreateTime(new Date());
		o.setLoginIp(this.Request.getClientIP());
		o.setMemberName(Member.getRealName());
		o.setMemberId(Member.getUID());
		o.setCompanyId(Member.getCompany());
		String companyName = new Q("select FullName from crm_company where ID = ?",o.getCompanyId()).executeString();
		o.setCompanyName(companyName);
		
		String companyMobile = new Q("select Mobile from ec_member where ID = ?",o.getCompanyId()).executeString();
		String memberMobile = new Q("select Mobile from ec_member where ID = ?",o.getMemberId()).executeString();
		o.setMemberMobile(memberMobile);
		o.setCompanyMobile(companyMobile);
		
		if ("Ent".equals(Member.getType())) {
			o.setSummary("一级账号");
		} else{
			String IsEngineer = new Q(" select IsEngineer from eao_is_engineer where Member = ?", o.getMemberId())
					.executeString();
			if (StringUtils.isEmpty(IsEngineer)) {
				o.setSummary("游客");
			} else {
				String position = new Q(" select Position from eao_Position where Id = ?", IsEngineer).executeString();
				o.setSummary(position);
			}
		}
		
		o.setOperation("登录系统");
		
		Integer dt = new Q("select count(1)+1 as num from eao_operation_log where MemberId = ?",Member.getUID()).executeInt();
		o.setCount(dt);
		transaction.insert(o);
		if (transaction.commit()) {
			s("插入成功");
		} else {
			f("插入失败");
		}
	}

}
